package com.jingwu.example.graphql;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.jingwu.example.pojo.entity.CategoryDO;
import com.jingwu.example.pojo.query.CategoryQuery;
import com.jingwu.example.pojo.vo.CategoryVO;
import com.jingwu.example.pojo.vo.Pagination;
import com.jingwu.example.repo.CategoryRepository;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.deepexi.web.support.pojo.converter.utils.ConverterUtils.convert;
import static com.deepexi.web.support.pojo.converter.utils.ConverterUtils.convertAll;

/**
 * @author jingwu
 * @since 2021/2/3
 */

@Service
public class CategoryQueryResolver implements GraphQLQueryResolver {

    private final CategoryRepository repository;

    public CategoryQueryResolver(CategoryRepository repository) {
        this.repository = repository;
    }

    public List<CategoryVO> listCategories(CategoryQuery query) {
        List<CategoryDO> list = repository.select(query);
        return convertAll(list, CategoryVO.class);
    }

    public Pagination<CategoryVO> getCategoryPage(CategoryQuery query) {
        Page<CategoryDO> page = repository.selectPage(query);
        return new Pagination<>(page.getTotal(), convertAll(page.getRecords(), CategoryVO.class));
    }

    public CategoryVO getCategory(Integer id) {
        CategoryDO entity = repository.selectById(id);
        return convert(entity, CategoryVO.class);
    }

}
